'''
Creates a basic configuration file
$LastChangedBy: csgillespie $
$LastChangedDate: 2007-06-06 11:08:06 +0000 (Wed, 06 Jun 2007) $
'''
import ConfigParser, os
import ErrorClasses

def createConfigFile():
	'''Creates a configuration file in /home/USERNAME/.basis'''
	file_path = os.path.expanduser('~/.pysbml')
	try:
		open(file_path, 'r')
		return True
	except IOError:
		fp = open(file_path, 'w+a')
		config = ConfigParser.SafeConfigParser()
		fp.close()
	config.add_section('Basis WS')
	config.set('Basis WS', 'username', None)
	config.set('Basis WS', 'password', None)
	config.set('Basis WS', 'model_urn', None)
	config.set('Basis WS', 'simulation_group_urn', None)
	config.set('Basis WS', 's_id', None)
	config.set('Basis WS', 'path', None)
	fp = open(file_path, 'a')
	config.write(fp)
	fp.close()

def addToConfigFile(dict_options):
	'''Adds new values to config file'''
	allowed_keys = ('username', 'password', 'model_urn', \
		'simulation_group_urn', 's_id', 'path')
	
	file_path = os.path.expanduser('~/.pysbml')
	config = ConfigParser.SafeConfigParser()
	config.readfp(open(file_path))
	for key, value in dict_options.items():
		if key not in allowed_keys:
			raise ErrorClasses.BasisConfigError('%s is an unrecognised option'\
				% key)
		config.set('Basis WS', key, value)
	
	#Write values to file
	fp = open(file_path, 'w')
	config.write(fp)
	fp.close()

def getConfigFile():
	'''Returns a dictionary of the configuration file'''
	config_dict = {}
	file_path = os.path.expanduser('~/.pysbml')
	config = ConfigParser.SafeConfigParser()
	config.readfp(open(file_path))
	for value in ('username', 'password', 'model_urn', \
		'simulation_group_urn', 's_id', 'path'):
		entry = config.get('Basis WS', value)
		if entry == 'None':
			entry = None
		config_dict[value] = entry
	return config_dict
	
if __name__ == '__main__':
	createConfigFile()
	addToConfigFile({'username':'colin'})
